package com.ah.http.factory;

import com.ah.http.resttemplate.SkipVerifyRestTemplateBuilder;
import com.ah.shanxi_demo.sync.param.result.SyncPolicyResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ah.shanxi_demo.common.StrUtils.syncObjectToJsonObject;
import static com.ah.http.resttemplate.RestTemplateUtil.params_header_convert_httpEntity;
import static com.ah.http.resttemplate.RestTemplateUtil.url_with_params;

/**
 * @author Tao on 2020/8/6
 */
public class HttpsFactory {

    private static final String Params="{\"code\":200,\"data\":[{\"guid\":\"string\",\"guid_type\":\"0\",\"policy_code\":\"1102-2020\",\"policy_list\":[{\"begin_time\":\"2020-02-02 02:27:37\",\"end_time\":\"2020-02-02 02:27:37\",\"log_type\":\"alarm\",\"policy_type\":\"ALM-01-2\"},{\"begin_time\":\"2020-02-02 02:27:37\",\"end_time\":\"2020-02-02 02:27:37\",\"log_type\":\"alarm\",\"policy_type\":\"ALM-01-1\"}]}],\"msg\":\"SUCCESS\"}";

    /**
     * https_post(各种传参类型)
     *
     * @param url
     * @param paramsMap
     * @param headersMap
     * @return
     */
    public static ResponseEntity https_post_rest_template(String url, Map<String, Object> paramsMap, Map<String, String> headersMap) {

        SkipVerifyRestTemplateBuilder model = new SkipVerifyRestTemplateBuilder();
        RestTemplate restTemplate = model.skipVerifyRestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, params_header_convert_httpEntity(paramsMap, headersMap), String.class);

        return stringResponseEntity;
    }

    /**
     * https_get
     *
     * @param url
     * @param paramsMap 参数
     * @param headersMap 请求头
     * @return
     */
    public static ResponseEntity https_get_rest_template(String url, Map<String, Object> paramsMap, Map<String, String> headersMap) {

        SkipVerifyRestTemplateBuilder model = new SkipVerifyRestTemplateBuilder();
        RestTemplate restTemplate = model.skipVerifyRestTemplate();

        //判断是否带参数  带参数
        if (!paramsMap.isEmpty()) {
            url = url_with_params(url, paramsMap);
        }

        //判断带请求头
        if (!headersMap.isEmpty()) {
            return restTemplate.exchange(url, HttpMethod.GET,
                    params_header_convert_httpEntity(paramsMap, headersMap), String.class);
        }
        return restTemplate.getForEntity(url, String.class, new Object[]{});
    }


    public static ResponseEntity https_post_rest_template_demo() {
        String url = "https://192.168.30.194/api/login";

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("username", "admin");
        paramsMap.put("password", "9c16ff1ecec9195cf8d30bec799d31ec5e09dda0");

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/x-www-form-urlencoded");
        headerMap.put("Cookie", "AMBARISESSIONID=63j5qyaaiq0yvmthw7dyj6ac; ailpha-bigdata=078239D653DDAA5C1402114D94332894");
        headerMap.put("Referer", "https://192.168.30.194/index.html");

        ResponseEntity responseEntity = https_post_rest_template(url, paramsMap, headerMap);
        return responseEntity;

    }

    public static ResponseEntity https_get_rest_template_demo() {
        String url = "https://192.168.30.246/api/license/info";

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Referer", "https://192.168.30.246/index.html");

        ResponseEntity responseEntity = https_get_rest_template(url, new HashMap<>(), headerMap);
        return responseEntity;

    }

    public static void main(String[] args) {

//        ResponseEntity responseEntity = https_post_rest_template_demo();
//        System.out.println(responseEntity);

//        ResponseEntity responseEntityMenuList = https_get_rest_template_demo();
//        JSONObject jsonObject = syncObjectToJsonObject(responseEntityMenuList.getBody());
//        System.out.println(jsonObject);


        JSONObject jsonObject1 = syncObjectToJsonObject(Params);

        String data = jsonObject1.get("data").toString();
        System.out.println(data);

        List<SyncPolicyResult> list = JSONObject.parseArray(jsonObject1.get("data").toString(), SyncPolicyResult.class);

        System.out.println(JSONObject.toJSONString(list));


    }


}
